package com.stylefeng.guns.modular.vpn.timer;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Calendar;
import java.util.Date;


public class StatisticsQuartzRunner {

    public static void start() throws SchedulerException {
        //创建调度器
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler scheduler = sf.getScheduler();
        //创建JobDetail，指定作业名和组，以及具体的类名
        JobDetail jobDetail = JobBuilder.newJob(StatisticsQuartzJob.class)
                .withIdentity("statisticsJob","statisticsGroup").build();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR,12);
        calendar.set(Calendar.MINUTE,10);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);
        Date startTime = calendar.getTime();

        SimpleTrigger simpleTrigger = TriggerBuilder.newTrigger()
                .withIdentity("statisticsTriggerName","statisticsTriggerGroup")
                .startAt(startTime)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .repeatForever()
                        .withIntervalInSeconds(24*60*60))
                .build();

        Date ft = scheduler.scheduleJob(jobDetail,simpleTrigger);

        System.out.printf("%s 这个任务会在 %s 准时开始运行，累计运行%d次，间隔时间是%d毫秒%n",
                jobDetail.getKey(), ft.toLocaleString(),simpleTrigger.getRepeatCount()+1, simpleTrigger.getRepeatInterval());


        scheduler.start();
    }
}
